﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.WebServices;
using CoolCode.Web.UI.Views;
using CoolCode.Services;

namespace CoolCode.Web.UI.Controls
{
    public partial class ExcelExporter : System.Web.UI.UserControl, IExporter
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
        }

        //private  IExportService<T> exportService;
        //private Func<List<T>> provideData;

        public void Setup<T>(IExportService<T> exportService, Func<List<T>> provideData)
        {
            if (Page is IAuthPage)
            {
                if (!((IAuthPage)Page).Role.AllowExport)
                {
                    this.Enabled = false;
                    return;
                }
            }

            lnkExcel.AsExportable(exportService,
                provideData,
                c => { ViewHelper.BindFilePath(lnkExcelSrc, c.FilePath); udpExcel.Update(); udpExcel.Alert(c.Message); },
                c => udpExcel.Alert(c.Message)
            );
        }

        public virtual bool Enabled
        {
            get
            {
                return lnkExcel.Enabled;
            }
            set
            {
                lnkExcel.Enabled = value;
            }
        }
    }
}